﻿using System;
using System.Collections.Generic;
using System.Linq;
namespace Manage.Member
{
    public class MemberMange
    {
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns></returns>
        public EntityCore.Member.MemberInfo MemberLogin(string Account, string memberPwd)
        {
            if (!Utils.RegexUtils.IsAccount(Account))
            {
                throw new ExceptionUtil("请输入正确的账号");
            }

            if (string.IsNullOrEmpty(memberPwd))
            {
                throw new ExceptionUtil("请输入正确的密码");
            }
            using (EntityCore.DBModels.DBContext context = new EntityCore.DBModels.DBContext())
            {
                EntityCore.DBModels.Member user = context.Member.FirstOrDefault(p => p.Account == Account && p.Password == memberPwd);
                if (user == null)
                {
                    throw new ExceptionUtil("账号或密码错误");
                }
                /// 创建用户信息
                return this.CreateToken(user);
            }
        }

        /// <summary>
        /// 创建token
        /// </summary>
        /// <returns></returns>
        public EntityCore.Member.MemberInfo CreateToken(EntityCore.DBModels.Member member)
        {
            if (member == null)
            {
                throw new ExceptionUtil("用户信息为空");
            }
            /// 手机和PC单点登录
            string token = System.Guid.NewGuid().ToString();
            EntityCore.Member.MemberInfo memberInfo = new EntityCore.Member.MemberInfo()
            {
                WechatId = member.WechatId,
                Avatar = member.Avatar,
                Gender = member.Gender,
                MemberId = member.MemberId,
                NickName = member.Nickname,
                Token = token
            };
            string redisKey = this.GetRedisKeyByPlatform(member.MemberId);
            bool exists = RedisHelper.HashService.HashExists(EnumCore.Utils.RedisKeyEnum.USER_TOKEN.ToString(), redisKey);
            if (exists == true)
            {
                // 如果存在 则发送消息给原有用户下线了
                RedisHelper.HashService.HashDelete(EnumCore.Utils.RedisKeyEnum.USER_TOKEN.ToString(), redisKey);
            }
            // 存如token信息
            bool setSuccess = RedisHelper.HashService.HashSet(EnumCore.Utils.RedisKeyEnum.USER_TOKEN.ToString(), redisKey, memberInfo);
            if (!setSuccess)
            {
                throw new ExceptionUtil("未知错误");
            }
            return memberInfo;
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <returns></returns>
        public bool MemberRegister(string account, string password, string avatar, string nickName)
        {
            if (!Utils.RegexUtils.IsAccount(account))
            {
                throw new ExceptionUtil("账号由4-12为的数字和字符组成");
            }
            else if (string.IsNullOrEmpty(password))
            {
                throw new ExceptionUtil("请输入正确的密码");
            }

            else if (!Utils.RegexUtils.IsNickName(nickName))
            {
                throw new ExceptionUtil("昵称由2-8位汉字、字母与数字的组成");
            }

            using (EntityCore.DBModels.DBContext context = new EntityCore.DBModels.DBContext())
            {
                /// 创建用户信息
                EntityCore.DBModels.Member user = new EntityCore.DBModels.Member()
                {
                    Account = account,
                    Avatar = avatar,
                    CreateTime = DateTime.Now,
                    Gender = 0,
                    Nickname = nickName,
                    Password = password,
                    RegisterWith = (int)this.GetPlatform(),
                    WechatId = account,
                    MemberId = System.Guid.NewGuid().ToString("N")
                };
                context.Add(user);
                if (context.SaveChanges() != 1)
                {
                    throw new ExceptionUtil("未知错误");
                }
                else
                {
                    return true;
                }
            }
        }

        /// <summary>
        /// 获取当前访问的平台
        /// </summary>
        /// <returns></returns>
        public EnumCore.Member.Platform GetPlatform()
        {
            /// 根据header获取平台
            string header = HttpContextUtil.Current.Request.Headers["platform"];
            int headerNumber;
            if (header == null || !int.TryParse(header, out headerNumber))
            {
                throw new ExceptionUtil("未知设备");
            }
            EnumCore.Member.Platform platform = (EnumCore.Member.Platform)headerNumber;
            return platform;
        }

        /// <summary>
        /// 根据使用平台获取redis_key
        /// </summary>
        /// <returns></returns>
        public string GetRedisKeyByPlatform(string memberId)
        {
            return (int)this.GetPlatform() + "_" + memberId;
        }
    }
}
